<?php 

class wxShareController extends appController
{
    
    /**
     * 显示分享列表
     */
    public function index()
    {
        $page = yf_empty(intval($_REQUEST['page']), 1, true);
        
        $wxshare = Model('wxShare');
        
        $list = $wxshare->getShareList($page);//获取分享列表
        $this->assign("art_list", $list['result']);
        $this->assign("page", $list['page']);
        
        $this->display('index.html');
    }
    
    /**
     * 插入分享信息
     */
    public function addShare()
    {
        $wxshare = Model('wxShare');
        
        if($_POST)
        {
            $res = array('error' => 1, 'content' => '添加失败');
            $info['share_title'] = yf_empty($_POST['share_title'], "请输入分享标题");
            $info['share_desc'] = yf_empty($_POST['share_desc'], "请输入分享描述");
            $info['share_url'] = yf_empty($_POST['share_url'], "请输入分享链接");
            if(!strpos($info['share_url'], '{{url}}'))
            {
                if(hy_check($info['share_url'], 'u') == '#@_error')
                {
                    $res['content'] = "输入的链接格式不正确";
                    $this->showMsg($res);
                }   
            }
            $info['share_logo'] = yf_empty($_POST['share_logo'], "请上传分享图片");
            $info['share_code'] = yf_empty($_POST['share_code'], "请输入唯一的英文标识");
            if(hy_check($info['share_code'], 'eg') == '#@_error')
            {
                $res['content'] = "输入标识格式不正确";
                $this->showMsg($res);
            }
            $isExist = $wxshare->checkCodeExist($info['share_code']);
            if($isExist)
            {
                $res['content'] = "输入的标识已存在";
                $this->showMsg($res);
            }
            $info['share_msg'] = yf_empty($_POST['share_msg'], "请输入备注说明");
            $info['created'] = yf_time();
            
            $id = $wxshare->insertShare($info);
            if($id)
            {
                $res = array('error' => 0, 'content' => '添加成功');
            }
            $this->showMsg($res);
        }
        
        $this->assign("action", "addShare");
        $this->display("share.html");
    }
    
    /**
     * 编辑分享信息
     */
    public function editShare()
    {
        $wxshare = Model('wxShare');
        
        if($_POST)
        {
            $res = array('error' => 1, 'content' => '修改失败');
            $id = yf_empty(intval($_POST['id']), "系统错误");
            $info['share_title'] = yf_empty($_POST['share_title'], "请输入分享标题");
            $info['share_desc'] = yf_empty($_POST['share_desc'], "请输入分享描述");
            $info['share_url'] = yf_empty($_POST['share_url'], "请输入分享链接");
            if(!strpos($info['share_url'], '{{url}}'))
            {
                if(hy_check($info['share_url'], 'u') == '#@_error')
                {
                    $res['content'] = "输入的链接格式不正确";
                    $this->showMsg($res);
                }   
            }
            $info['share_logo'] = yf_empty($_POST['share_logo'], "请上传分享图片");
            $info['share_code'] = yf_empty($_POST['share_code'], "请输入唯一的英文标识");
            if(hy_check($info['share_code'], 'eg') == '#@_error')
            {
                $res['content'] = "输入标识格式不正确";
                $this->showMsg($res);
            }
            $isExist = $wxshare->checkCodeExist($info['share_code'], $id);
            if($isExist)
            {
                $res['content'] = "输入的标识已存在";
                $this->showMsg($res);
            }
            $info['share_msg'] = yf_empty($_POST['share_msg'], "请输入备注说明");
            $info['created'] = yf_time();
            
            $id = $wxshare->updateShare($info, $id);
            if($id)
            {
                $res = array('error' => 0, 'content' => '修改成功');
            }
            $this->showMsg($res);
        }
        
        $id = yf_empty(intval($_GET['id']), "系统错误");
        $this->assign("id", $id);
        
        $info = $wxshare->getShareInfo($id);
        $this->assign("info", $info);
        
        $this->assign("action", "editShare");
        $this->display("share.html");
    }
    
    /**
     * 删除分享信息
     */
    public function delShare()
    {
        $wxshare = Model('wxShare');
        $res = array('error' => 1, 'content' => '删除失败');
        $id = yf_empty(intval($_REQUEST['id']), "系统错误");
        $del = $wxshare->delShare($id);//删除分享信息
        if($del)
        {
            $res = array('error' => 0, 'content' => '删除成功');
        }
        $this->showMsg($res);
    }
    
}